package com.yyydjk.library.fragments;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;
import com.yyydjk.library.R;
import com.yyydjk.library.activitys.LibraryListActivity;
import com.yyydjk.library.base.BaseListFragment;
import com.yyydjk.library.event.SkinChangeEvent;
import com.yyydjk.library.value.AppValue;
import com.yyydjk.library.value.NetValue;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * Created by Administrator on 2018/4/9 0009.
 */

public class JiaoChengFragment extends BaseListFragment {
    private BaseQuickAdapter baseQuickAdapter;
    List<AVObject> datas;

    @Override
    protected void initDatas() {
//        获取服务器数据
        AVQuery<AVObject> avQuery = new AVQuery<>(NetValue.LIBRARY_CLASSTABLE);
        avQuery.whereEqualTo(NetValue.LIBRARY_CLASSTYPE, "JiaoCheng");
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e==null){
                    datas=list;
                    if(datas.size()>0){
                        if(baseQuickAdapter==null){
                            initAdapter();
                        }else {
                            baseQuickAdapter.setNewData(datas);
                            baseQuickAdapter.notifyDataSetChanged();
                        }
                        multipleStatusView.showContent();
                        swipeRefreshLayout.setRefreshing(false);
                    }else {
                        multipleStatusView.showEmpty();
                    }

                }else {
                    multipleStatusView.showNoNetwork();
                }
            }
        });
    }
    private void initAdapter(){
        baseQuickAdapter=new BaseQuickAdapter<AVObject,BaseViewHolder>(getItemLayout(),datas) {

            @Override
            protected void convert(BaseViewHolder helper, AVObject item) {
                TextView textView = helper.getView(R.id.text);
                textView.setText(item.getString("libraryClassName"));
                setIconDrawable(textView, MaterialDesignIconic.Icon.gmi_book);
            }
        };
        baseQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(), LibraryListActivity.class);
                intent.putExtra(AppValue.LIBRARYLISTACTIVITY_TITLE,datas.get(position).getString("libraryClassName"));
                intent.putExtra(AppValue.LIBRARYLISTACTIVITY_LIBRARYTYPE,datas.get(position).getString("libraryClassType"));
                intent.putExtra(AppValue.LIBRARYLISTACTIVITY_CLASSTYPE,datas.get(position).getString("classType"));
                startActivity(intent);
            }
        });
        baseQuickAdapter.isFirstOnly(false);
        baseQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        recyclerView.setAdapter(baseQuickAdapter);
    }
    @Override
    protected int getItemLayout() {
        return R.layout.item_common;
    }
    //    刷新列表颜色
    @Subscribe
    public void onEvent(SkinChangeEvent event){
        if(baseQuickAdapter!=null){
            baseQuickAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {
        initDatas();
    }
}
